﻿
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');

FantastPlatform.UI.FlowElementPanel = function (config) {
    Ext.apply(this, config);
    var newWidth = this.width;
    var newHeight = this.height;
    var thispoliceNum = this.policemanNumber;
    var Level = this.Level;
    var FLowId = this.FlowId;
    var LevelId = this.Id;
    var sexStore = new Ext.data.SimpleStore({
        fields: ['Name', 'Value'],
        data: [['A级加强', '1'], ['A级', '2'], ['B级', '3'], ['C级', '4'], ['D级', '5']]
    });
    var levelComboBox = new Ext.form.ComboBox({
        fieldLabel: '预案级别',
        name: 'FlowLevel',
        hiddenName: 'FlowLevel',
        store: sexStore,
        displayField: 'Name',
        mode: 'local',
        valueField: 'Value',
        allowBlank: false,
        editable: false,
        triggerAction: 'all',
        emptyText: '选择预案级别',
        readOnly: true,
        anchor: '90%'
    });
    function updataLevelForm(form, v) {
        form.form.load({
            url: '../Service/FlowElementViewService.aspx?Action=GetLevelInfo&flowId=' + FLowId,
            params: { level: v },
            waitMsg: '正在读取数据...',
            waitTitle: '请稍后...',
            failure: function (form, action) {
                if (action.failureType != 'client') {
                    showErrorMessage('操作失败', action.result.message, '');
                }
            },
            success: function (form, action) {
                var reText = action.response.responseText;
                var reTextObj = Ext.util.JSON.decode(reText);
                if (eval(reTextObj.success)) {
                    form.findField('Range').setValue(reTextObj.data.Range);
                    form.findField('PersonNumber').setValue(reTextObj.data.PersonNumber);
                }
            }
        });
    }
    levelComboBox.on('select', function () {
        Level = levelComboBox.getValue();
        elementGrid.FlowLevel = Level;
        updataLevelForm(uLevelInfo, Level);
        elementGrid.onUpdataFlowElementClick(Level);
    });
    var elementGrid = new FantastPlatform.UI.FlowElementGrid({
        FlowId: FLowId,
        FlowLevel: Level,
        url: '../Service/FlowElementViewService.aspx?Action=Search&flowId=' + FLowId,
        height: newHeight - 5,
        level: 0,
        border: false,
        tbar: [{
            text: '新增警力分配',
            iconCls: 'add',
            tooltip: {
                text: '新增警力分配',
                autoHide: true
            },
            handler: function () { elementGrid.onAddFlowElementClick(elementGrid.FlowId, elementGrid.FlowLevel); }
        }, '-', {
            text: '编辑',
            iconCls: 'edit',
            tooltip: {
                text: '选择要编辑的应急预案',
                autoHide: true
            },
            handler: function () { elementGrid.onEditFlowElementClick(); }
        }, '-', {
            text: '查询',
            iconCls: 'search',
            tooltip: {
                text: '按照条件查询应急预案',
                title: '查询通知',
                autoHide: true
            },
            handler: function () { elementGrid.onSearchClick(elementGrid.FlowId, elementGrid.FlowLevel); }
        }, '-', {
            text: '删除',
            iconCls: 'delete',
            tooltip: {
                text: '删除选中的应急预案',
                title: '删除通知',
                autoHide: true
            },
            handler: function () { elementGrid.onDeleteClick(); }
        }, '-', {
            text: '刷新',
            iconCls: 'updata',
            tooltip: {
                text: '重新更新列表内容',
                title: '列表刷新',
                autoHide: true
            },
            handler: function () { elementGrid.onUpdataFlowElementClick(elementGrid.FlowLevel); }
        }]
    });
    this.ElementGrid = elementGrid;
    elementGrid.addListener('rowdblclick', function () { elementGrid.onEditFlowElementClick(); });
    elementGrid.onUpdataFlowElementClick(Level);
    var uLevelInfo = new Ext.form.FormPanel({
        border: false,
        split: true,
        style: 'border:solid 1px',
        labelWidth: 70,
        height: newHeight - 5,
        labelAlign: 'right',
        bodyStyle: 'padding:5px 5px 0',
        collapsible: true,
        tbar: [{
            text: '保存',
            iconCls: 'save',
            id: 'saveLevel',
            handler: function () { onAddLevelRangeClick(uLevelInfo); }
        }],
        items: [{
            layout: 'column',
            border: false,
            labelSeparator: '：',
            items: [{
                columnWidth: 1,
                border: false,
                layout: 'form',
                items: [levelComboBox]
            },{
                    columnWidth: 1,
                    border: false,
                    layout: 'form',
                    items: [new Ext.form.TextField({
                        fieldLabel: '预计人数',
                        name: 'PersonNumber',
                        allowBlank: false,
                        blankText: " 预计人数不能为空"                      
                    })]
                }, {
                columnWidth: 1,
                border: false,
                layout: 'form',
                items: [new Ext.form.TextArea({
                    fieldLabel: '说明',
                    name: 'Range',
                    height: 200,
                    allowBlank: true,
                    anchor: '90%'
                })]
            }]
        }]
    });

    this.items = [{
        columnWidth: .3,
        height: newHeight,
        border: false,
        items: [uLevelInfo]
    }, {
        columnWidth: .7,
        height: newHeight,
        border: false,
        items: [elementGrid]
    }];



    function onAddLevelRangeClick(form) {
        if (form.form.isValid()) {
            var dataStr1 = form.form.getValues().Range;
            var dataStr2 = form.form.getValues().PersonNumber;
            Ext.Msg.confirm('提示', '确认要更改该级别的适用警情状态吗？', function (btn) {
                if (btn == 'yes') {
                    Ext.Msg.wait('正在街道到警员中……', '添加');
                    //alert(fIds);
                    Ext.Ajax.request({
                        method: 'post',
                        url: '../Service/FlowElementViewService.aspx?Action=AddLevelRange&flowId=' + FLowId,
                        params: { id: LevelId, Range: dataStr1, level: Level, personNumber: dataStr2 },
                        failure: function (form, action) {
                            if (action.failureType != 'client') {
                                showErrorMessage('操作失败', action.result.message, '');
                            }
                        },
                        success: function (request, options) {
                            // 服务器返回json形式的结果                                          
                            var reText = request.responseText;
                            var reTextObj = Ext.util.JSON.decode(reText);
                            if (eval(reTextObj.success)) {
                                showMessage("更改成功", reTextObj.message);
                            }
                        }
                    });
                }
            })
        }
    }
    FantastPlatform.UI.FlowElementPanel.superclass.constructor.call(this, {
        layout: 'column',
        border: false
    });
};

Ext.extend(FantastPlatform.UI.FlowElementPanel, Ext.Panel, {

});